//
//  CRootViewController.m
//  ZQReadProject
//
//  Created by 2.5 on 2021/9/7.
//

#import "CRootViewController.h"
#import "CHomeViewController.h"
#import "CCHQViewController.h"
#import "CCMineViewController.h"
#import "CCHeader.h"
#import "HZMarketViewController.h"
#import "HZNetworkTool.h"
#import "CHttpManager.h"
#import "AppDelegate.h"
#import "CCWebViewViewController.h"
#import "CLoginViewController.h"
#import "CCInfoViewController.h"
@interface CRootViewController ()
@property (nonatomic,assign)CGPoint startPoint;
@property (nonatomic,assign)CGPoint centerPoint;
@property (nonatomic,strong)UIView *leftView;
@property (nonatomic,strong)UIImageView *userImageView;
@property (nonatomic,strong)UILabel *userLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UILabel *sexLabel;
@property (nonatomic,strong)UILabel *infoLabel;
@property (nonatomic,strong)UIButton *loginoutButton;
@property (nonatomic,assign)BOOL canScroll;
@end

@implementation CRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customeCCUI];
    self.canScroll = NO;
    [self showAlertAction];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

            switch (status) {

                case AFNetworkReachabilityStatusNotReachable:{

                    NSLog(@"无网络");

                    break;

                }

                case AFNetworkReachabilityStatusReachableViaWiFi:{

                    [self showAlertAction];

                    break;

                }

                case AFNetworkReachabilityStatusReachableViaWWAN:{

                    [self showAlertAction];

                    break;

                }

                default:

                    break;

            }

        }];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.canScroll = NO;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.canScroll = YES;
}
-(void)showAlertAction
{
    [CHttpManager requestGET:@"https://lh7fb4lk.engine.lncldglobal.com/1.1/classes/review/6136db65ed028f2ed89dec8c" andParam:@{} andSuccess:^(NSDictionary * _Nonnull reslutDic) {
        
        [[NSUserDefaults  standardUserDefaults] setObject:NotNilStr(reslutDic[@"review"]) forKey:@"review"];
        
        [[NSUserDefaults  standardUserDefaults] setObject:NotNilStr(reslutDic[@"color"]) forKey:@"color"];
        [[NSUserDefaults  standardUserDefaults] setObject:NotNilStr(reslutDic[@"bottom"]) forKey:@"bottom"];
        [[NSUserDefaults  standardUserDefaults] setObject:NotNilStr(reslutDic[@"top"]) forKey:@"top"];
        [[NSUserDefaults  standardUserDefaults] setObject:NotNilStr(reslutDic[@"url"]) forKey:@"url"];
        if([reslutDic[@"is_privacy_agreement"] boolValue]){
            AppDelegate *delegate =  (AppDelegate *) [UIApplication sharedApplication].delegate;
            if([delegate isKindOfClass:[AppDelegate class]] ){
                CCWebViewViewController *cvc= [[CCWebViewViewController alloc] init];
                cvc.baseUrl = NotNilStr(reslutDic[@"privacy_url"]);
                cvc.hidesBottomBarWhenPushed = YES;
                
                delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:cvc];
            }
        }
        
    } andFailed:^(NSError * _Nullable error, NSDictionary * _Nullable resultDic) {
        
    }];
}
-(void)customeCCUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    CHomeViewController *hvc = [[CHomeViewController alloc] init];
    hvc.title = @"news";
    UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:hvc];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"news" image:[UIImage imageNamed:@"tab1"] selectedImage:[UIImage imageNamed:@"tab1"]];
    nvc1.tabBarItem = item1;
    
    
    HZMarketViewController *hqVC = [[HZMarketViewController alloc] init];
    hqVC.title = @"market";
    UINavigationController *hqNav = [[UINavigationController alloc] initWithRootViewController:hqVC];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"market" image:[UIImage imageNamed:@"tab2"] selectedImage:[UIImage imageNamed:@"tab2"]];
    hqNav.tabBarItem = item2;
    
//    CCHQViewController *hqvc = [[CCHQViewController alloc] init];
//    hqvc.title = @"行情";
//    UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:hqvc];
//    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"行情" image:[UIImage imageNamed:@"home_test"] selectedImage:[UIImage imageNamed:@"home_test_selected"]];
//    nvc2.tabBarItem = item2;
    [HZNetworkTool shareNetworkTool];
    CCMineViewController *mvc = [[CCMineViewController alloc] init];
    mvc.title = @"setting";
    UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:mvc];
    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"setting" image:[UIImage imageNamed:@"tab3"] selectedImage:[UIImage imageNamed:@"tab3"]];
    nvc3.tabBarItem = item3;
    
    self.viewControllers = @[nvc1, hqNav , nvc3];
    
    UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showOrHideLeftAction:)];
    [self.view addGestureRecognizer:pgr];
    
    self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight)];
    self.leftView.backgroundColor = UIColorFromRGB(0xf8f8f8);
    [self customeLeftUI];
}
-(void)customeLeftUI
{
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
    
    self.userImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, [CQTool statusBarHeight]+40 , 40, 40)];
    self.userImageView.image = [UIImage imageNamed:@"head_default44"];
    self.userImageView.layer.masksToBounds = YES;
    self.userImageView.layer.cornerRadius = 20;
    [self.leftView addSubview:self.userImageView];
    
    self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.userImageView.ccx+self.userImageView.ccwidth +10, self.userImageView.ccy, self.view.ccwidth/2.0 -(self.userImageView.ccx+self.userImageView.ccwidth +10), 40)];
    if(token&&token.length){
        self.userLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"name"]);
    }else{
        self.userLabel.text = @"Login In";
    }
    self.userLabel.font = [UIFont systemFontOfSize:15];
    self.userLabel.textColor = UIColorFromRGB(0x121212);
    [self.leftView addSubview:self.userLabel];
    self.userLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tgr =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginAction)];
    [self.userLabel addGestureRecognizer:tgr];
    [self.userLabel addGestureRecognizer:tgr];
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.userImageView.ccy +55, self.view.ccwidth/2.0, 20)];
    self.sexLabel.font = [UIFont systemFontOfSize:15];
    if(token&&token.length){
        self.sexLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"sex"]);
    }else{
        self.sexLabel.text = @"--";
    }
    
    [self.leftView addSubview:self.sexLabel];
    
    self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,self.userImageView.ccy + 80, self.view.ccwidth/2.0, 100)];
    self.infoLabel.numberOfLines = 0;
    self.infoLabel.font = [UIFont systemFontOfSize:15];
    self.infoLabel.textColor = [UIColor lightGrayColor];
    if(token&&token.length){
        self.infoLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"info"]);
    }else{
        self.infoLabel.text = @"--";
    }
    
    [self.leftView addSubview:self.infoLabel];
    
    self.loginoutButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.ccheight-50, self.view.ccwidth/2.0, 50)];
    self.loginoutButton.backgroundColor = [UIColor clearColor];
    [self.loginoutButton setTitle:@"sign out" forState:UIControlStateNormal];
    [self.loginoutButton addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
    [self.loginoutButton setTitleColor: UIColorFromRGB(0x121212) forState:UIControlStateNormal];
    if([CQTool statusBarHeight]>20){
        self.loginoutButton.frame = CGRectMake(0, self.view.ccheight-50 - 40, self.view.ccwidth/2.0, 50+40);
    }
    [self.leftView addSubview:self.loginoutButton];
    
}
-(void)showOrHidLeftAction
{
    if(self.selectedIndex ==2 || !self.canScroll ){
        return;
    }
    [self.view.superview insertSubview:self.leftView belowSubview:self.view];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if(self.view.center.x==self.view.ccwidth/2.0){
        self.view.center = CGPointMake(self.view.ccwidth, self.view.ccheight/2.0);
    }else{
        self.view.center = CGPointMake(self.view.ccwidth/2.0, self.view.ccheight/2.0);
    }
    [UIView commitAnimations];
}
-(void)showOrHideLeftAction:(UIPanGestureRecognizer *)pgr
{
    if(self.selectedIndex ==2 || !self.canScroll ){
        return;
    }
    NSLog(@"%@",self.view.superview);
    if(pgr.state == UIGestureRecognizerStateBegan){
        self.startPoint = [pgr locationInView:self.view.superview];
        self.centerPoint = self.view.center;
        [self.view.superview insertSubview:self.leftView belowSubview:self.view];
    }else if(pgr.state == UIGestureRecognizerStateChanged){
        CGPoint point = [pgr locationInView:self.view.superview];
        CGPoint newPoint = CGPointMake(self.centerPoint.x + (point.x - self.startPoint.x), self.centerPoint.y);
        if(newPoint.x <= self.view.ccwidth &&newPoint.x>=self.view.ccwidth/2.0){
            self.view.center = newPoint;
        }
        
    }else if(pgr.state == UIGestureRecognizerStateEnded || pgr.state == UIGestureRecognizerStateCancelled){
        if(self.view.center.x>= self.view.ccwidth/4.0*3){
            self.view.center = CGPointMake(self.view.ccwidth, self.centerPoint.y);
        }else{
            self.view.center = CGPointMake(self.view.ccwidth/2.0, self.centerPoint.y);
        }
        
    }
}
-(void)loginAction
{
    [self showOrHidLeftAction];
   NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
   if(!token||!token.length){
       CCLoginViewController *cvc = [[CCLoginViewController alloc] init];
       cvc.title = @"login";
       cvc.hidesBottomBarWhenPushed = YES;
       [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cvc] animated:YES completion:nil];
   }else{
       CCInfoViewController *cvc = [[CCInfoViewController alloc] init];
       cvc.title=@"info";
       [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cvc] animated:YES completion:nil];
   }
}
     -(void)logoutAction
     {
         NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
         if(token&&token.length){
        UIAlertController *avc =[UIAlertController alertControllerWithTitle:@"alert" message:@"Are you sure to exit?" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cctoken"];
            [self showOrHidLeftAction];
            [self reloadData];
        }];
        [avc addAction:sure];
        [avc addAction:cancel];
        [self presentViewController:avc animated:YES completion:nil];
        }
    }
-(void)reloadData
{
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"cctoken"];
    if(token&&token.length){
        self.userLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"name"]);
        self.sexLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"sex"]);
        self.infoLabel.text = NotNilStr([[NSUserDefaults standardUserDefaults] objectForKey:@"info"]);
    }else{
        self.userLabel.text = @"Login In";
        self.sexLabel.text = @"--";
        self.infoLabel.text = @"--";
    }
}
@end
